{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #验证回文串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isPalindrome"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #验证回文串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个字符串 <code>s</code> ，验证 <code>s</code>&nbsp;是否是&nbsp;<strong>回文串&nbsp;</strong>，只考虑字母和数字字符，可以忽略字母的大小写。</p>\n",
    "\n",
    "<p>本题中，将空字符串定义为有效的&nbsp;<strong>回文串&nbsp;</strong>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入: </strong>s =<strong> </strong>&quot;A man, a plan, a canal: Panama&quot;\n",
    "<strong>输出:</strong> true\n",
    "<strong>解释：</strong>&quot;amanaplanacanalpanama&quot; 是回文串</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> s = &quot;race a car&quot;\n",
    "<strong>输出:</strong> false\n",
    "解释：&quot;raceacar&quot; 不是回文串</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 2 * 10<sup>5</sup></code></li>\n",
    "\t<li>字符串 <code>s</code> 由 ASCII 字符组成</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><meta charset=\"UTF-8\" />注意：本题与主站 125&nbsp;题相同：&nbsp;<a href=\"https://leetcode-cn.com/problems/valid-palindrome/\">https://leetcode-cn.com/problems/valid-palindrome/</a></p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [XltzEq](https://leetcode.cn/problems/XltzEq/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [XltzEq](https://leetcode.cn/problems/XltzEq/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        tmpstr = \"\"\n",
    "        for i in s:\n",
    "            if i.isdigit() or i.isalpha():\n",
    "                tmpstr += i.lower()\n",
    "        if tmpstr == tmpstr[::-1]:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s):\n",
    "        strs = \"\"\n",
    "        for i in s:\n",
    "            if i.isalnum():\n",
    "                strs += i.lower()\n",
    "        return strs == strs[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s1 = ''\n",
    "        for i in s:\n",
    "            if i.isalpha() or i.isdigit():\n",
    "                s1 += i.lower()\n",
    "        return s1 == s1[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        l, r = 0, len(s)-1\n",
    "        while l<r:\n",
    "            lc = s[l]\n",
    "            rc = s[r]\n",
    "            if not lc.isdigit() and not lc.isalpha():\n",
    "                l += 1\n",
    "                continue\n",
    "            if not rc.isdigit() and not rc.isalpha():\n",
    "                r -= 1\n",
    "                continue\n",
    "            lc = lc.lower()\n",
    "            rc = rc.lower()\n",
    "            if lc!=rc:\n",
    "                return False\n",
    "            l += 1\n",
    "            r -= 1\n",
    "            \n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        lens = len(s)\n",
    "        left, right = 0, lens - 1\n",
    "        while (left < right): # stay cycle\n",
    "            while left < right and not s[left].isalnum(): # judge\n",
    "                left +=1\n",
    "            while left < right and not s[right].isalnum():\n",
    "                right -=1\n",
    "            if s[left].lower() != s[right].lower() :\n",
    "                return False\n",
    "            left ,right = left +1,right-1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        \n",
    "        # left, right = 0, len(s) - 1\n",
    "        # while left < right:\n",
    "        #     while not 'a' <= s[left].lower() <='z' and left < right: left+=1\n",
    "        #     while not 'a' <= s[right].lower() <='z' and left < right: right-=1\n",
    "\n",
    "        #     if s[left].lower() != s[right].lower():\n",
    "        #         return False\n",
    "        #     left += 1\n",
    "        #     right -= 1\n",
    "        # return True\n",
    "        strs = \"\"\n",
    "        for i in s:\n",
    "            if i.isalnum():\n",
    "                strs += i.lower()\n",
    "        return strs == strs[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        l, r = 0, len(s)-1\n",
    "        while l<r:\n",
    "            lc = s[l]\n",
    "            rc = s[r]\n",
    "            if not lc.isalnum():\n",
    "                l += 1\n",
    "                continue\n",
    "            if not rc.isalnum():\n",
    "                r -= 1\n",
    "                continue\n",
    "            lc = lc.lower()\n",
    "            rc = rc.lower()\n",
    "            if lc!=rc:\n",
    "                return False\n",
    "            l += 1\n",
    "            r -= 1\n",
    "            \n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s=s.lower()\n",
    "        a=[\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\n",
    "        \"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\",\"0\",\n",
    "        \"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]\n",
    "        d=\"\"\n",
    "        for i in range(len(s)):\n",
    "           if s[i] in a:\n",
    "               d+=s[i]\n",
    "        if d==d[::-1]:\n",
    "            return True\n",
    "        return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        n=len(s)\n",
    "        a=''\n",
    "        for i in range(n):\n",
    "            if (s[i]>='a' and s[i]<='z') or (s[i]>='A' and s[i]<='Z') or (s[i]>='0' and s[i]<='9'):\n",
    "                a+=s[i]\n",
    "        a=a.lower()\n",
    "        b=a[::-1]\n",
    "        print(a)\n",
    "        if a==b:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = s.lower()\n",
    "        translator = str.maketrans('', '', string.punctuation)\n",
    "        s = s.translate(translator)\n",
    "        s = s.replace(\" \", \"\")\n",
    "        s1 = s[::-1]\n",
    "        if s == s1:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s=s.lower()\n",
    "        s1=''\n",
    "        for i in s:\n",
    "            if i.isdigit() or i.isalpha():\n",
    "                s1+=i\n",
    "        if s1==s1[::-1]:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        arr = []\n",
    "        s = s.lower()\n",
    "        for x in s:\n",
    "            if x.isalpha() or x.isdigit():\n",
    "                arr.append(x)\n",
    "        s = ''.join(arr)\n",
    "        print(s)\n",
    "        return s == s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "       # 1\n",
    "        # l,r = 0,len(s)-1\n",
    "        # while l<r:\n",
    "        #     while not s[l].isalnum() and l < r:\n",
    "        #         l += 1\n",
    "        #     while not s[r].isalnum() and l < r:\n",
    "        #         r -= 1\n",
    "        #     if s[l].lower() != s[r].lower():\n",
    "        #         return False\n",
    "        #     l += 1\n",
    "        #     r -= 1\n",
    "        # return True\n",
    "\n",
    "        # return \"\".join(re.findall(r'[a-z0-9]*',s.lower())) == \"\".join(re.findall(r'[a-z0-9]*',s.lower()))[::-1]\n",
    "\n",
    "        return re.sub(r'[^0-9a-z]', '', s.lower()) == re.sub(r'[^0-9a-z]', '', s.lower())[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "#    #1\n",
    "#     def isPalindrome(self, s: str) -> bool:\n",
    "#         left,right = 0, len(s) - 1\n",
    "#         while left < right:\n",
    "#             if not s[left].isalnum():\n",
    "#                 left += 1\n",
    "#                 continue\n",
    "#             if not s[right].isalnum():\n",
    "#                 right -= 1\n",
    "#                 continue\n",
    "#             if s[left].lower() == s[right].lower():\n",
    "#                 left += 1\n",
    "#                 right -= 1\n",
    "#             else:\n",
    "#                 break\n",
    "        \n",
    "#         return right <= left\n",
    "    \n",
    "    #2 利用语言特性\n",
    "     def isPalindrome(self, s: str) -> bool:\n",
    "         s = ''.join(i for i in s if i.isalnum()).lower()\n",
    "         return s == s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = s.lower()\n",
    "        st = \"\"\n",
    "        for i in s:\n",
    "            if 'a'<= i <= 'z' or '0'<= i <= '9':\n",
    "                st=st+i\n",
    "        deque = list(st)\n",
    "        while len(deque)>1:\n",
    "            front = deque.pop()\n",
    "            back = deque.pop(0)\n",
    "            if front != back:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        \n",
    "        s = s.lower()\n",
    "        string=\"\".join(filter(str.isalnum, s))\n",
    "        # print(string)\n",
    "        return string == string[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = \"\".join([i for i in s if i not in string.punctuation and i!=\" \"]).lower()\n",
    "        num = len(s)\n",
    "        i = 0\n",
    "        j = num - 1\n",
    "        if len(s) == 0:\n",
    "            return True\n",
    "        while (i<j) and (s[i]==s[j]):\n",
    "            i = i+1\n",
    "            j = j-1\n",
    "        if s[i]!=s[j]:\n",
    "            return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        re_s = str.lower(\"\".join(re.findall(\"[0-9a-zA-Z]+\", s)))\n",
    "        return re_s == re_s[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        re_s = str.lower(\"\".join(re.findall(\"[0-9a-zA-Z]+\", s)))\n",
    "        lens = len(re_s) \n",
    "        for i in range(int(lens/2)):\n",
    "            if re_s[i] != re_s[lens -1 - i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        re_s = str.lower(\"\".join(re.findall(\"[0-9a-zA-Z]+\", s)))\n",
    "        lens = len(re_s) \n",
    "        for i in range(int(lens/2)):\n",
    "            if re_s[i] != re_s[lens -1 - i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        char = []\n",
    "        s = s.lower()\n",
    "        for i in s:\n",
    "            if i.isalnum():\n",
    "                char.append(i)\n",
    "        if(char == char[::-1]):\n",
    "            return True\n",
    "        else:   \n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        snew = re.sub(r'[^0-9a-zA-Z]+','',s).lower()\n",
    "        if snew == snew[::-1]:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        char = []\n",
    "        s = s.lower()\n",
    "        for i in s:\n",
    "            if i.isalnum():\n",
    "                char.append(i)\n",
    "        if(char == char[::-1]):\n",
    "            return True\n",
    "        else:   \n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        char = []\n",
    "        s = s.lower()\n",
    "        for i in s:\n",
    "            if i.isalnum():\n",
    "                char.append(i)\n",
    "        if(char == char[::-1]):\n",
    "            a = 1\n",
    "        else:   \n",
    "            a = 0\n",
    "        return bool(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s_new = re.sub(r'[^a-zA-Z0-9]', \"\", s)\n",
    "        return s_new.lower() == s_new.lower()[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        li = ''.join(re.findall(r'[a-z0-9]*', s.lower()))\n",
    "        i, j = 0, len(li) - 1\n",
    "\n",
    "        return li == li[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        new = re.sub('[^a-z0-9]','', s.lower())\n",
    "        print(new)\n",
    "        if new == new[::-1]:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        if s==\"\": return True\n",
    "        news=[x for x in s.lower() if (x.isnumeric() or x>='a' and x <= 'z')]\n",
    "        center= len(news)%2 and len(news)//2+1 or len(news)//2\n",
    "        front=news[0:len(news)//2]\n",
    "        rear=news[center:len(news)][::-1]\n",
    "        return front == rear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        if len(s)==0:return True\n",
    "        \n",
    "        s=s.lower()\n",
    "        s =list(s)\n",
    "        k=0\n",
    "        for i in range(len(s)):\n",
    "            if not s[i]:\n",
    "                continue\n",
    "            s[k]=s[i]\n",
    "            k+=1\n",
    "        s=s[:k+1]\n",
    "\n",
    "\n",
    "        \n",
    "        # s=s.lower()\n",
    "        count=0\n",
    "        for i in range(len(s)):\n",
    "            if not self.isS(s[i]):\n",
    "                count+=1\n",
    "        if count==len(s):return True\n",
    "\n",
    "        left = 0\n",
    "\n",
    "        right =len(s)-1 \n",
    "        while left < right:\n",
    "            while right>0 and not self.isS(s[right]):right-=1\n",
    "            while left < len(s)-1 and not self.isS(s[left]):left+=1\n",
    "            if s[left]!=s[right]:\n",
    "                return False\n",
    "            left+=1\n",
    "            right-=1\n",
    "        return True\n",
    "    \n",
    "    def isS(slef,ss):\n",
    "        if ss >='0' and ss<='9': #首先判断是否数字\n",
    "            return True \n",
    "        if ss<'a' or ss>'z':\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        hui_s = []\n",
    "        for ss in s:\n",
    "            if (ss >= 'a' and ss <='z') or (ss >= '0' and ss <= '9'):\n",
    "                hui_s.append(ss)\n",
    "            elif ss >= 'A' and ss <= 'Z':\n",
    "                hui_s.append(ss.lower())\n",
    "        if hui_s[::-1] == hui_s:\n",
    "            return True\n",
    "        else: \n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        tmp = []\n",
    "        for c in s:\n",
    "            if ord(c) >= ord('A') and ord(c) <= ord('Z'):\n",
    "                tmp.append(c.lower())                \n",
    "            elif ord(c) >= ord('a') and ord(c) <= ord('z'):\n",
    "                tmp.append(c)\n",
    "            elif ord(c) >= ord('0') and ord(c) <= ord('9'):\n",
    "                tmp.append(c)\n",
    "        print(tmp)\n",
    "        i = 0\n",
    "        j = len(tmp) - 1\n",
    "        while i < j:\n",
    "            if tmp[i] != tmp[j]:\n",
    "                return False\n",
    "            i += 1\n",
    "            j -= 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = s.lower()\n",
    "        res = []\n",
    "        tmp = []\n",
    "        for letter in s:\n",
    "            if (letter >= 'a' and letter <= 'z') or (letter>='0' and letter<='9') :\n",
    "                res += letter\n",
    "                tmp.insert(0,letter)\n",
    "        print(res, tmp)\n",
    "        if ''.join(res) ==  ''.join(tmp) :\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        hui_s = []\n",
    "        for ss in s:\n",
    "            if (ss >= 'a' and ss <='z') or (ss >= '0' and ss <= '9'):\n",
    "                hui_s.append(ss)\n",
    "            elif ss >= 'A' and ss <= 'Z':\n",
    "                hui_s.append(ss.lower())\n",
    "        print(hui_s)\n",
    "        if hui_s[::-1] == hui_s:\n",
    "            return True\n",
    "        else: \n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = ''.join(s.split(' '))\n",
    "        s= s.lower()\n",
    "       \n",
    "        res = ''.join(list(filter(lambda x:x.isalnum(),list(s))))\n",
    "        if res == res[::-1]:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        # 先把字符串进行一次清洗，把字母和数字字符挑选出来，放到一个新列表中。\n",
    "        s_list = list()\n",
    "        for i in range(len(s)):\n",
    "            if (s[i] >= \"0\" and s[i] <= \"9\") or (s[i]  >= \"A\" and s[i]  <= \"Z\"):\n",
    "                s_list.append(s[i])\n",
    "            elif s[i]  >= \"a\" and s[i] <= \"z\":\n",
    "                s_list.append(s[i].upper())\n",
    "        stri = \"\".join(s_list)\n",
    "        return self.isSimplePalindrome(stri)\n",
    "    \n",
    "    def isSimplePalindrome(self, s):\n",
    "        # 先实现最基本的回文判断\n",
    "        left, right = 0, len(s) - 1\n",
    "        while left < right:\n",
    "            if s[left] != s[right]:\n",
    "                return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = \"\".join([x.lower() for x in s if x.isalnum() ])\n",
    "        len_s = len(s) \n",
    "        # if len_s % 2 == 0:\n",
    "        # for i in range(int(len_s/2)):\n",
    "        #     if s[i] != s[len(s) - i-1]:\n",
    "        #         return False\n",
    "        return s == s[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        trans = \"\".join([ss.lower() for ss in s if ss.isalnum()])\n",
    "        return trans == trans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        cur = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        left, right = 0, len(cur) - 1\n",
    "        while left < right:\n",
    "            if cur[left] != cur[right]:\n",
    "                return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        '''就是双指针,怎么判断字符都可以吧\n",
    "        '''\n",
    "        s = ''.join([x.lower() for x in s if x.isalnum() ])\n",
    "        # print(s)\n",
    "        i ,j = 0,len(s)-1\n",
    "        while (i<j):\n",
    "            if s[i]!=s[j]: return False\n",
    "            i+=1\n",
    "            j-=1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood=\"\".join(char.lower() for char in s if char.isalnum())\n",
    "        return sgood==sgood[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood = ''.join(ch.lower() for ch in s if ch.isalnum())\n",
    "        n = len(sgood)\n",
    "        left, right = 0, n - 1\n",
    "\n",
    "        while left < right:\n",
    "            if sgood[left] != sgood[right]:\n",
    "                return False\n",
    "            left,right = left + 1, right - 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        # return 'a1'.isalnum()\n",
    "        # return '1'.lower()\n",
    "        if not s:\n",
    "            return True\n",
    "        result = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i].isalnum():\n",
    "                result.append(s[i].lower())\n",
    "        n = len(result)\n",
    "        # return result\n",
    "        for i in range(n//2):\n",
    "            if not result[i] == result[n-1-i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        print(s)\n",
    "        if len(s)==1:\n",
    "            return True\n",
    "        left, right = 0, len(s) - 1\n",
    "        while left <= right:\n",
    "            l = s[left]\n",
    "            r = s[right]\n",
    "            print(l, r)\n",
    "            if l == r:\n",
    "                left+=1\n",
    "                right-=1\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "        # sgood = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        # return sgood==sgood[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood = ''.join(ch.lower() for ch in s if ch.isalnum() )\n",
    "        return sgood == sgood[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        return sgood == sgood[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self,s:str):\n",
    "        s_clean=[]    \n",
    "        for i in s:\n",
    "            if i.isalnum():\n",
    "                s_clean.append(i.lower())\n",
    "        left,right=0,len(s_clean)-1\n",
    "        while left<right:\n",
    "            if s_clean[left]!=s_clean[right]:\n",
    "                return False\n",
    "            else:\n",
    "                left+=1\n",
    "                right-=1\n",
    "        return True\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s):\n",
    "        number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n",
    "        chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',\n",
    "                 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',\n",
    "                 'y', 'z', 'A', \"B\", 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',\n",
    "                 'K', 'L', \"M\", 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',\n",
    "                 'W', 'X', 'Y', 'Z']\n",
    "        s = [s[i].lower() for i in range(len(s)) if s[i] in number or s[i] in chars]\n",
    "        mid = (len(s) - 1) >> 1\n",
    "        stack = []\n",
    "        if len(s) % 2 == 1:\n",
    "            for i in range(mid):\n",
    "                stack.append(s[i])\n",
    "            for i in range(mid + 1, len(s)):\n",
    "                if stack.pop(-1) != s[i]:\n",
    "                    return False\n",
    "        else:\n",
    "            for i in range(mid + 1):\n",
    "                stack.append(s[i])\n",
    "            for i in range(mid + 1, len(s)):\n",
    "                if stack.pop(-1) != s[i]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s= \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        print(s)\n",
    "        for i in range(int(len(s)/2)):\n",
    "\n",
    "            if s[i] != s[len(s)-1-i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        def func(s):\n",
    "            s = str(s)\n",
    "            if s.isalpha:\n",
    "                return s.lower()\n",
    "        s = ''.join(list(map(func, [i for i in s if i.isalpha() or i.isdigit()])))\n",
    "        return s == s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def isPalindrome(self, s: str) -> bool:\n",
    "#         l, r = 0, len(s) - 1\n",
    "#         while l < r:\n",
    "#             while (l < r) and not(('a' <= s[l].lower() <= 'z') or ('0' <= s[l] <= '9')):\n",
    "#                 l += 1\n",
    "#             while (l < r) and not(('a' <= s[r].lower() <= 'z') or ('0' <= s[r] <= '9')):\n",
    "#                 r -= 1\n",
    "#             if l >= r:\n",
    "#                 return True\n",
    "#             if s[l].lower() != s[r].lower():\n",
    "#                 return False\n",
    "#             l += 1\n",
    "#             r -= 1\n",
    "#         return True\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        l = []\n",
    "        for letter in s:\n",
    "            if 'a' <= letter.lower() <= 'z' or '0' <= letter <= '9':\n",
    "                l.append(letter.lower())\n",
    "        return l == l[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        cur = []\n",
    "        for i in s:\n",
    "            if i.isdigit() or i.isalpha():\n",
    "                cur.append(i.upper())\n",
    "        return cur == cur[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        l = [ch.lower() for ch in s if ch.isalnum()]\n",
    "        return l == l[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        arr = []\n",
    "        for x in s:\n",
    "            if  x.isalpha() or x.isdigit():\n",
    "                if x.isalpha():\n",
    "                    arr.append(x.lower())\n",
    "                else:\n",
    "                    arr.append(x)\n",
    "        print(arr)\n",
    "        if arr == arr[::-1]:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        n=len(s)\n",
    "        low,high=0,n-1\n",
    "        s_new=\"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        return s_new == s_new[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s1=\"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        return s1==s1[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood = ''.join(ch.lower() for ch in s if ch.isalnum())\n",
    "        n = len(sgood)\n",
    "        left, right = 0, n-1\n",
    "\n",
    "        while left<right:\n",
    "            if sgood[left] != sgood[right]:\n",
    "                return False\n",
    "            left, right = left+1, right-1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        return sgood == sgood[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        # # 方法1：筛选 + 判断\n",
    "        # ns = ''\n",
    "        # for ch in s:\n",
    "        #     if ch.isalnum():\n",
    "        #         ns += ch.lower()\n",
    "        # return ns == ns[::-1]\n",
    "\n",
    "        # 字符串、双指针\n",
    "        ns = ''.join(ch.lower() for ch in s if ch.isalnum())  # join生成器\n",
    "        left, right = 0, len(ns) - 1\n",
    "        while left < right:\n",
    "            if ns[left] != ns[right]:\n",
    "                return False\n",
    "            else:\n",
    "                left += 1\n",
    "                right -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        sgood = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        return sgood == sgood[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isPalindrome(self, s: str) -> bool:\n",
    "        s = \"\".join(ch.lower() for ch in s if ch.isalnum())\n",
    "        n = len(s)\n",
    "        left = 0\n",
    "        right = n -1\n",
    "\n",
    "        while left < right:\n",
    "            if s[left] != s[right]:\n",
    "                return False\n",
    "            left = left + 1\n",
    "            right = right- 1\n",
    "        return True"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
